package com.abel.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.abel.web.view.RegistrationViewBean;

public class RegistrationViewValidator implements Validator {

	@SuppressWarnings("rawtypes")
	@Override
	public boolean supports(Class clazz) {
		return RegistrationViewBean.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {
		
		RegistrationViewBean viewBean = (RegistrationViewBean)obj;
		
		if (viewBean.getUserName() == null 
				|| viewBean.getUserName().equals("")) {
			errors.rejectValue("userName", "error.null",null,"User Name is required field");
		}
		//TODO - validate for special chars
		if (viewBean.getPassword() == null 
				|| viewBean.getPassword().equals("")) {
			errors.rejectValue("password", "error.null",null,"Password is required field");
		}
		//TODO - validate for some special chars
		if (viewBean.getConfirmPassword() == null 
				|| viewBean.getConfirmPassword().equals("")) {
			errors.rejectValue("confirmPassword", "error.null",null,"Confirm password is required field");
		}
		
		if (viewBean.getPassword() != null &&
				!viewBean.getPassword().equals(viewBean.getConfirmPassword())) {
			errors.rejectValue("confirmPassword", "error.null",null,"Confirm password and password to not match");
		}
		
		if (viewBean.getEmail() == null 
				|| viewBean.getEmail().equals(""))  {
			errors.rejectValue("email", "error.null",null,"Email address is required field");

		}
		
//		if (viewBean.getEmail() != null 
//				&& !viewBean.getEmail().equals("") 
//				&& !viewBean.getEmail().matches("[^A-Za-z0-9\\.\\@_\\-~#]+"))  {
//			errors.rejectValue("email", "error.null",null,"Email address is required");
//
//		}
	}
}
